import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono
import java.net.URLDecoder

object Crawler {
    class RedirectException(var url: String) : Throwable()

    private fun String.decodeUri() = URLDecoder.decode(this, "UTF-8")!!
    fun request(url: String): Mono<String> =
            WebClient.create(url).get()
//                    .headers {
//                it.accept = listOf(MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_XML)
//                it.acceptLanguage = listOf(Locale.LanguageRange("zh-CN", 0.8))
//                it["Accept-Encoding"] = "gzip"
//                it["Referer"] = "http://www.example.com/"
//                it["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
//            }
                    .retrieve().onStatus({ status ->
                        status.value() == 302
                    }) { res ->
                        val location = res.headers().header("Location").first()
                        println(location)
                        val uri = location.decodeUri()
                        println(uri)
                        RedirectException(uri).toMono()
                    }.bodyToMono(String::class.java)
                    .onErrorResume {
                        if (it is RedirectException) {
                            this.request(it.url)
                        } else
                            Mono.error(it)
                    }
//                    .exchange().map { it.headers() }
//                    .retrieve().bodyToMono(String::class.java)
}

fun main(args: Array<String>) {
    println("https://login.taobao.com/jump?target=https%253a%252f%252fdetail.tmall.com%252fitem.htm%253fid%253d588763869289%2526tbpm%253d1".replace("%25", "%"))
}